<template>
  <div>
    <el-row>
      <el-col :span="8" style="padding-right: 10px">
        <el-card class="box-card">
          <div class="user">
            <img v-if="photo" src="../assets/images/头像.jpg" />
            <img v-else src="../assets/images/user.png" />
            <div class="userinfo">
              <p class="name">{{name}}</p>
              <p class="access">{{identity}}</p>
            </div>
          </div>
          <div class="login-info">
            <p>上次登录时间：<span>2022-12-10</span></p>
            <p>上次登录地点：<span>南宁</span></p>
          </div>
        </el-card>
        <el-card style="margin-top: 20px; height: 460px">
          <el-table :data="tableData" style="width: 100%">
            <!-- <el-table-column
                        prop="name"
                        label="手机种类">
                    </el-table-column>
                    <el-table-column
                        prop="todayBuy"
                        label="今日购买">
                    </el-table-column>
                    <el-table-column
                        prop="monthBuy"
                        label="本月购买">
                    </el-table-column>
                    <el-table-column
                        prop="totalBuy"
                        label="总购买">
                    </el-table-column> -->

            <!-- 使用对象遍历的方法得到表 -->
            <el-table-column
              v-for="(val, key) in tableLabel"
              :prop="key"
              :label="val"
              :key="val"
            />
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="16" style="padding-left: 10px">
        <div class="num">
          <el-card
            v-for="item in countData"
            :key="item.name"
            :body-style="{ display: 'flex', padding: 0 }"
          >
            <i
              class="icon"
              :class="`el-icon-${item.icon}`"
              :style="{ background: item.color }"
            ></i>
            <div class="detail">
              <p class="price">{{ item.value }}</p>
              <p class="desc">{{ item.name }}</p>
            </div>
          </el-card>
        </div>
        <el-card style="height: 280px">
          <!-- 折线图 -->
          <div ref="chartOne" style="height: 280px"></div>
        </el-card>
        <div class="graph">
          <el-card style="height: 260px">
            <!-- 柱状图 -->
            <div ref="chartTwo" style="height: 280px"></div>
          </el-card>
          <el-card style="height: 260px">
            <!-- 饼状图 -->
            <div ref="chartThree" style="height:240px;"></div>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getData } from "../api";
import * as echarts from "echarts"; //安装引入指定版本：npm i echarts@5.1.2
// echarts:一个基于 JavaScript 的开源可视化图表库:https://echarts.apache.org/zh/index.html
export default {
  name: "Home",
  data() {
    return {
      tableData: [],
      tableLabel: {
        name: "课程",
        todayBuy: "今日购买",
        monthBuy: "本月购买",
        totalBuy: "总购买",
      },
      countData: [
        {
          name: "今日支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今日收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "今日未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
        {
          name: "本月支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "本月收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "本月未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
      ],
    };
  },
  computed:{
    name(){
      var abb = this.$store.state.tab.menu
      if (abb.length > 2) {
        return 'Admin'
      }else{
        return 'User'
      }
    },
    identity(){
      var abb = this.$store.state.tab.menu
      if (abb.length > 2) {
        return '超级管理员'
      }else{
        return '用户'
      }
    },
    photo(){
      var abb = this.$store.state.tab.menu
      if (abb.length > 2) {
        return true
      }else{
        return false
      }
    }
  },
  mounted() {
    getData().then(({ data }) => {
      //解构 (加上{}代表要从它里面拿出数据)
      const { tableData } = data.data; //取出data下的data中的tableData
      console.log(tableData);
      this.tableData = tableData;
      console.log(data);

      // 折线图
      // 基于准备好的dom，初始化echarts实例
      const ChartOne = echarts.init(this.$refs.chartOne);
      // 指定图表的配置项和数据
      var ChartOneOption = {};
      // 处理数据xAxis
      const { orderData } = data.data;
      const xAxis = Object.keys(orderData.data[0]); //该方法会返回对象中的键名
      ChartOneOption.xAxis = { data: xAxis };
      // 处理数据xAxis
      ChartOneOption.yAxis = {};
      // 处理图例legend
      ChartOneOption.legend = {
        data: xAxis,
      };
      // 具体需要渲染的数据
      ChartOneOption.series = [];
      xAxis.forEach((key) => {
        ChartOneOption.series.push({
          name: key,
          data: orderData.data.map((item) => item[key]), //从orderData.data中拿出与item[key]对应的数据
          type: "line",
        });
      });
      console.log(ChartOneOption);
      // 使用刚指定的配置项和数据显示图表
      ChartOne.setOption(ChartOneOption);

      // 柱状图
      const {userData} = data.data
      const ChartTwo = echarts.init(this.$refs.chartTwo);
      const ChartTwoOption = {
        legend: {
          // 图例文字颜色
          textStyle: {
            color: "#333",
          },
        },
        grid: {
          left: "20%",
        },
        // 提示框
        tooltip: {
          trigger: "axis",
        },
        xAxis: {
          type: "category", // 类目轴
          data: userData.map(item=>item.date), //取出userData中每一项的date
          axisLine: {
            lineStyle: {
              color: "#17b3a3",
            },
          },
          axisLabel: {
            interval: 0,
            color: "#333",
          },
        },
        yAxis: [
          {
            type: "value",
            axisLine: {
              lineStyle: {
                color: "#17b3a3",
              },
            },
          },
        ],
        color: [
          "#2ec7c9",
          "#b6a2de",
          "#5ab1ef",
          "#ffb980",
          "#d87a80",
          "#8d98b3",
        ],
        series: [
          {
            name:'新增用户',
            data:userData.map(item => item.new),
            type:'bar'
          },
          {
            name:'活跃用户',
            data:userData.map(item => item.active),
            type:'bar'
          }
        ],
      };
      ChartTwo.setOption(ChartTwoOption)

      // 饼状图
      const {videoData} = data.data
      const ChartThree = echarts.init(this.$refs.chartThree);
      const ChartThreeOption = {
         tooltip: {
            trigger: "item",
          },
          color: [
            "#0f78f4",
            "#dd536b",
            "#9462e5",
            "#a6a6a6",
            "#e1bb22",
            "#39c362",
            "#3ed1cf",
          ],
          series: [
            {
              data:videoData,
              type:'pie'
            }
          ],
      }
      ChartThree.setOption(ChartThreeOption)
    });
  },
};
</script>

<style lang='less' scoped>
.user {
  padding-bottom: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid #ccc;
  display: flex;
  align-items: center;
  img {
    margin-right: 30px;
    width: 150px;
    height: 150px;
    border-radius: 50%;
  }
  .userinfo {
    .name {
      font-size: 32px;
      margin-top: 20px;
    }
    .access {
      color: #999999;
    }
  }
}
.login-info {
  p {
    line-height: 28px;
    font-size: 14px;
    color: #999999;
  }
  span {
    color: #666666;
    margin-left: 60px;
  }
}

.num {
  display: flex;
  flex-wrap: wrap; //强制换行（多余部分自动换行）
  justify-content: space-around;
  .icon {
    width: 80px;
    height: 80px;
    font-size: 30px;
    line-height: 80px;
    text-align: center;
    color: white;
  }
  .detail {
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin-left: 15px;
    .price {
      font-size: 30px;
      margin-bottom: 10px;
      height: 30px;
      line-height: 30px;
    }
    .desc {
      font-size: 14px;
      color: #999999;
      text-align: center;
    }
  }
  .el-card {
    width: 32%;
    margin-bottom: 20px;
  }
}

.graph {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  .el-card {
    width: 48%;
  }
}
</style>